package com.gongpingjialoan.view;

import com.gongpingjialoan.R;
import com.gongpingjialoan.utility.DhUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class DashboardView extends RelativeLayout {

	Paint mPaint;
	int progresswidth;

	private int padding = 50;

	private int startAngle = -45;

	private int endAndle = 225;

	private int generalAngle = 60;

	private int betterAngle = 60;

	private int bestAngle = 60;

	Bitmap bit;

	private Matrix panRotate = new Matrix();

	Context context;
	
	double r;
	
	String gengerText = "一般";
	String betterText = "挺好";
	String bestText = "优秀";

	public DashboardView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		progresswidth = DhUtil.dip2px(context, 20);
		mPaint.setStrokeWidth(progresswidth);
		progresswidth = progresswidth / 2 + 2;
		mPaint.setStyle(Paint.Style.STROKE);
		setWillNotDraw(false);
		
	}

	public void setStrokeWidth(int StrokeWidth) {
		mPaint.setStrokeWidth(StrokeWidth);
		progresswidth = StrokeWidth / 2 + 2;
		setWillNotDraw(false);
	}

	public void setGeneralAngle(int generalAngle) {
		this.generalAngle = generalAngle;
		setWillNotDraw(false);
	}

	public void setBetterAngle(int betterAngle) {
		this.betterAngle = betterAngle;
		setWillNotDraw(false);
	}

	public void setBestAngle(int bestAngle) {
		this.bestAngle = bestAngle;
		setWillNotDraw(false);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// 画弧线 
		drawBottomTwoArc(canvas);
		// drawPointer(canvas);
		RectF oval = new RectF(progresswidth + padding,
				progresswidth + padding, getWidth() - progresswidth - padding,
				getHeight() - progresswidth - padding);
		mPaint.setColor(getResources().getColor(R.color.progress_orange));
		canvas.drawArc(oval, 179, generalAngle, false, mPaint);

		mPaint.setColor(getResources().getColor(R.color.progress_green));
		canvas.drawArc(oval, 178 + generalAngle, betterAngle, false, mPaint);

		mPaint.setColor(getResources().getColor(R.color.progress_blue));
		canvas.drawArc(oval, 177 + generalAngle + betterAngle, bestAngle + 3,
				false, mPaint);
		r = (getWidth()-padding*2) / 2;
		drawGenerText(canvas);
		drawBetterText(canvas);
		drawBestText(canvas);
		
//		canvas.drawPosText(text, pos, paint)

	}

	// private void drawPointer(Canvas canvas) {
	// Drawable draw = getResources().getDrawable(R.drawable.pointer);
	// bit = DhUtil.drawableToBitmap(draw);
	// panRotate.setRotate(x, px, py)
	// canvas.drawBitmap(bit, getWidth() / 2,
	// getHeight() / 2 - bit.getHeight(), null);
	//
	// canvas.drawBi
	// }

	// 绘制下发2个定死的弧度
	private void drawBottomTwoArc(Canvas canvas) {
		mPaint.setColor(getResources().getColor(R.color.progress_green_light));
		RectF oval = new RectF(progresswidth + padding,
				progresswidth + padding, getWidth() - progresswidth - padding,
				getHeight() - progresswidth - padding);
		canvas.drawArc(oval, 0, 35, false, mPaint);

		mPaint.setColor(getResources().getColor(R.color.progress_red));
		canvas.drawArc(oval, 145, 35, false, mPaint);
	}
	
	
	//获取一般弧度的右边的点的坐标
	private double[] getGenerRightPoint()  {
		double[] point= new double[2];
		point[0] = r - Math.cos((2 * Math.PI / 360) * generalAngle)*r+padding;
		point[1] = r - Math.sin((2 * Math.PI / 360) * generalAngle)*r+padding;
		return point;
		
	}
	
	//获取优秀弧度的左边的点的坐标
	private double[] getBestLeftPoint()  {
		double[] point= new double[2];
		point[0] = r + Math.cos((2 * Math.PI / 360) * bestAngle)*r+padding;
		point[1] = r - Math.sin((2 * Math.PI / 360) * bestAngle)*r+padding;
		return point;
			
	}
	
	
	//绘制一般的文字
	private void drawGenerText(Canvas canvas) {
		double right_point[] = getGenerRightPoint();
		
		double left_x = padding;
		double left_y = r+padding;
		
		float center_poinr[] = { (float) ((right_point[0]+left_x) / 2), (float) ((right_point[1] + left_y) / 2) };
		Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setTextSize(35);
		textPaint.setColor(Color.BLACK);
		FontMetrics fontMetrics = textPaint.getFontMetrics();  
		float fontTotalHeight = fontMetrics.bottom - fontMetrics.top; 
		float offY = fontTotalHeight / 2 - fontMetrics.bottom;  
		float newY = center_poinr[1] + offY;  
		canvas.drawText(gengerText, center_poinr[0]-(textPaint.measureText(gengerText))-progresswidth, newY, textPaint); 
	}
	
	//绘制挺好的文字
	private void drawBetterText(Canvas canvas) {
		double left_point[] = getGenerRightPoint();
		double right_point[] = getBestLeftPoint();
			
		float center_poinr[] = { (float) ((right_point[0]+left_point[0]) / 2), (float) ((right_point[1]+left_point[1]) / 2) };
		Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setTextSize(35);
		textPaint.setColor(Color.BLACK);
		FontMetrics fontMetrics = textPaint.getFontMetrics();  
		float fontTotalHeight = fontMetrics.bottom - fontMetrics.top; 
		float offY = fontTotalHeight / 2 - fontMetrics.bottom;  
		float newY = center_poinr[1] + offY;  
		canvas.drawText(betterText, center_poinr[0]-(textPaint.measureText(betterText)/2), center_poinr[1]-progresswidth, textPaint); 
	}
	
	
	//绘制最好的文字
	private void drawBestText(Canvas canvas) {
		double left_point[] = getBestLeftPoint();
		double right_point_x = 2*r+padding;
		double right_point_y = r+padding;		
		float center_poinr[] = { (float) ((right_point_x+left_point[0]) / 2), (float) ((right_point_y+left_point[1]) / 2) };
		Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setTextSize(35);
		textPaint.setColor(Color.BLACK);
		FontMetrics fontMetrics = textPaint.getFontMetrics();  
		float fontTotalHeight = fontMetrics.bottom - fontMetrics.top; 
		float offY = fontTotalHeight / 2 - fontMetrics.bottom;  
		float newY = center_poinr[1] + offY;  
		canvas.drawText(bestText, center_poinr[0]+progresswidth, newY, textPaint); 
	}
	
}
